package net.xxcxcxcx.xc_enhancedmob.register.client;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientEntityEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;
import net.xxcxcxcx.xc_enhancedmob.component.BaseComponent;
import net.xxcxcxcx.xc_enhancedmob.component.EnhancedBaseComponent;
import net.xxcxcxcx.xc_enhancedmob.data.MobData;
import net.xxcxcxcx.xc_enhancedmob.entity.SaveFireballEntity;
import net.xxcxcxcx.xc_enhancedmob.entity.SaveFireworkRocketEntity;
import net.xxcxcxcx.xc_enhancedmob.packet.CustomNbtPacket;
import net.xxcxcxcx.xc_enhancedmob.register.EntityRegister;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.BossBarsRendererHelper;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.complete.client.ClientCustomBossBarComplete;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.complete.sever.SeverCustomBossBarComplete;
import net.xxcxcxcx.xc_enhancedmob.render.damage_num.DamageNumParticle;
import net.xxcxcxcx.xc_enhancedmob.render.damage_num.DamageNumRenderer;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static net.xxcxcxcx.xc_enhancedmob.register.client.NetworkingRegister.*;

@Environment(value = EnvType.CLIENT)
public class ClientEventRegister {
    @Environment(value = EnvType.CLIENT)
    public static void ClientRegister() {
        ClientEntityEvents.ENTITY_LOAD.register((entity, world) -> {
            if (entity instanceof MobEntity mobEntity) {
                List<Text> list = moretitlepacket.get(mobEntity.getEntityId());
                if (list != null) {
                    BaseComponent baseComponent = ((MobData) mobEntity).getComponent(EnhancedBaseComponent.class.getName());
                    if (baseComponent instanceof EnhancedBaseComponent enhancedBaseComponent) {
                        enhancedBaseComponent.setMoreTitle(list);
                    }
                    moretitlepacket.remove(mobEntity.getEntityId());
                }
            }
        });
        ClientTickEvents.START_WORLD_TICK.register((world) -> {
            customentitylist.forEach((spawnPacket) -> {
                Entity entity2;
                Entity entity;
                int id = spawnPacket.id;
                UUID uuid = spawnPacket.uuid;
                EntityType<?> entityType = spawnPacket.entityType;
                double x = spawnPacket.x;
                double y = spawnPacket.y;
                double z = spawnPacket.z;
                byte pitch = spawnPacket.pitch;
                byte yaw = spawnPacket.yaw;
                int entityData = spawnPacket.entityData;
                short velocityX = spawnPacket.velocityX;
                short velocityY = spawnPacket.velocityY;
                short velocityZ = spawnPacket.velocityZ;
                if (entityType == EntityRegister.SaveFireballEntityType) {
                    entity = new SaveFireballEntity(world, x, y, z, velocityX, velocityY, velocityZ);
                    entity2 = world.getEntityById(entityData);
                    if (entity2 != null) {
                        ((SaveFireballEntity) entity).setOwner(entity2);
                    }
                } else if (entityType == EntityRegister.SaveFireworkRocketType) {
                    entity = new SaveFireworkRocketEntity(world, x, y, z, velocityX, velocityY, velocityZ);
                    entity2 = world.getEntityById(entityData);
                    if (entity2 != null) {
                        ((SaveFireworkRocketEntity) entity).setOwner(entity2);
                    }
                } else entity = null;
                if (entity != null) {
                    entity.updateTrackedPosition(x, y, z);
                    entity.refreshPositionAfterTeleport(x, y, z);
                    entity.pitch = (float) (pitch * 360) / 256.0f;
                    entity.yaw = (float) (yaw * 360) / 256.0f;
                    entity.setEntityId(id);
                    entity.setUuid(uuid);
                    world.addEntity(id, entity);
                }
            });
            customentitylist.clear();
        });
        ClientTickEvents.START_WORLD_TICK.register((world) -> {
            customParticlePackets.forEach((particle) -> {
                particle.Dotick(world);
            });
            customParticlePackets.removeIf((particle) -> !particle.CanUse(world));
        });
        ClientTickEvents.START_WORLD_TICK.register((world) -> {
            List<CustomNbtPacket> list = new ArrayList<>(customMobNbt);
            for (CustomNbtPacket packet : list) {
                for (String s : packet.nbt.getKeys()) {
                    if (s.equals("Bossbar")) {//Bossbar数据更新
                        NbtCompound nbtCompound = packet.nbt.getCompound(s);
                        if (nbtCompound.contains("kind")) {
                            String ss = nbtCompound.getString("kind");
                            nbtCompound.remove("kind");
                            nbtCompound.remove("visible");
                            if (ss.equals(SeverCustomBossBarComplete.UpdataType.ALL.toString())) {
                                BossBarsRendererHelper.addBossBar(new ClientCustomBossBarComplete(packet.id, nbtCompound));
                            } else if (ss.equals(SeverCustomBossBarComplete.UpdataType.REMOVE.toString())) {
                                BossBarsRendererHelper.removeBossBar(packet.id);
                            } else if (ss.equals(SeverCustomBossBarComplete.UpdataType.UPDATA.toString())) {
                                BossBarsRendererHelper.UpdataBossBar(packet.id, nbtCompound);
                            }
                        }
                    }
                    if (s.equals("DamageNum")) {//DamageNum数据更新
                        NbtCompound nbtCompound = packet.nbt.getCompound(s);
                        String name = nbtCompound.getString("DamageType");
                        float amount = nbtCompound.getFloat("amount");
                        Entity entity = world.getEntityById(nbtCompound.getInt("entity"));
                        if (entity != null) {
                            DamageNumParticle particle = new DamageNumParticle(entity, amount, name);
                            DamageNumRenderer.PARTICLES.add(particle);
                        }
                    }
                }
                customMobNbt.remove(packet);
            }
            for (ClientCustomBossBarComplete complete : BossBarsRendererHelper.getRenderList()) {
                if (world.getEntityById(complete.getEntity()) instanceof MobEntity mobEntity) {
                    complete.tick(mobEntity);
                } else complete.tick(null);
            }
        });
    }
}
